<?php
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	#CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id
	
	+ Module  		: m_unit Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_m_unit.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 11:40:14
	
*/

//class of m_unit
class C_m_unit extends CI_Controller {

	//constructor
	function __construct(){
		parent::__construct();
		
		$this->load->model('m_m_unit', '', TRUE);
		checkLogin();
	}
	
	//set index
	function index(){
		$this->load->view('main/v_m_unit');
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->m_unit_list();
				break;
			case "UPDATE":
				$this->m_unit_update();
				break;
			case "CREATE":
				$this->m_unit_create();
				break;
			case "DELETE":
				$this->m_unit_delete();
				break;
			case "SEARCH":
				$this->m_unit_search();
				break;
			case "PRINT":
				$this->m_unit_print();
				break;
			case "EXCEL":
				$this->m_unit_export_excel();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	//function fot list record
	function m_unit_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_m_unit->m_unit_list($query,$start,$end);
		echo $result;
	}
	
	//function for create new record
	function m_unit_create(){
		//POST varible here
		//auto increment, don't accept anything from form values
		$unit_name=trim(@$_POST["unit_name"]);
		$unit_name=htmlentities($unit_name,ENT_QUOTES);
		$unit_active=trim(@$_POST["unit_active"]);
		$unit_active=htmlentities($unit_active,ENT_QUOTES);
		$unit_created_by=$_SESSION[SESSION_USERID];
		$result=$this->m_m_unit->m_unit_create($unit_name ,$unit_active ,$unit_created_by );
		echo $result;
	}
	
	
	//function for update record
	function m_unit_update(){
		//POST variable here
		$unit_id=@$_POST["unit_id"];
		$unit_id=is_numeric($unit_id)?$unit_id:0;
		$unit_name=trim(@$_POST["unit_name"]);
		$unit_name=htmlentities($unit_name,ENT_QUOTES);
		$unit_active=trim(@$_POST["unit_active"]);
		$unit_active=htmlentities($unit_active,ENT_QUOTES);
		$unit_updated_date=date(LONG_FORMATDATE);
		$unit_updated_by=$_SESSION[SESSION_USERID];
		$result = $this->m_m_unit->m_unit_update($unit_id,$unit_name,$unit_active,$unit_updated_date,$unit_updated_by);
		echo $result;
	}
	
	//function for delete selected record
	function m_unit_delete(){
		$ids = @$_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_m_unit->m_unit_delete($pkid);
		echo $result;
	}

	//function for advanced search
	function m_unit_search(){
		//POST varibale here
		$unit_id=@$_POST["unit_id"];
		$unit_id=is_numeric($unit_id)?$unit_id:0;
		$unit_name=trim(@$_POST["unit_name"]);
		$unit_name=htmlentities($unit_name,ENT_QUOTES);
		$unit_active=trim(@$_POST["unit_active"]);
		$unit_active=htmlentities($unit_active,ENT_QUOTES);
		$unit_created_date=trim(@$_POST["unit_created_date"]);
		$unit_created_by=trim(@$_POST["unit_created_by"]);
		$unit_created_by=htmlentities($unit_created_by,ENT_QUOTES);
		$unit_updated_date=trim(@$_POST["unit_updated_date"]);
		$unit_updated_by=trim(@$_POST["unit_updated_by"]);
		$unit_updated_by=htmlentities($unit_updated_by,ENT_QUOTES);
		$unit_updated_count=@$_POST["unit_updated_count"];
		$unit_updated_count=is_numeric($unit_updated_count)?$unit_updated_count:0;
		
		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$result = $this->m_m_unit->m_unit_search($unit_id ,$unit_name ,$unit_active ,$unit_created_date ,$unit_created_by ,$unit_updated_date ,$unit_updated_by ,$unit_updated_count ,$start,$end);
		echo $result;
	}


	function m_unit_print(){
  		//POST varibale here
		$unit_id=@$_POST["unit_id"];
		$unit_id=is_numeric($unit_id)?$unit_id:0;
		$unit_name=trim(@$_POST["unit_name"]);
		$unit_name=htmlentities($unit_name,ENT_QUOTES);
		$unit_active=trim(@$_POST["unit_active"]);
		$unit_active=htmlentities($unit_active,ENT_QUOTES);
		$unit_created_date=trim(@$_POST["unit_created_date"]);
		$unit_created_by=trim(@$_POST["unit_created_by"]);
		$unit_created_by=htmlentities($unit_created_by,ENT_QUOTES);
		$unit_updated_date=trim(@$_POST["unit_updated_date"]);
		$unit_updated_by=trim(@$_POST["unit_updated_by"]);
		$unit_updated_by=htmlentities($unit_updated_by,ENT_QUOTES);
		$unit_updated_count=@$_POST["unit_updated_count"];
		$unit_updated_count=is_numeric($unit_updated_count)?$unit_updated_count:0;
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$data["data_print"] = $this->m_m_unit->m_unit_print($unit_id ,$unit_name ,$unit_active ,$unit_created_date ,$unit_created_by ,$unit_updated_date ,$unit_updated_by ,$unit_updated_count ,$option,$filter);
		$print_view=$this->load->view("main/p_m_unit.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/m_unit_printlist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';        
	}
	/* End Of Function */

	/* Function to Export Excel document */
	function m_unit_export_excel(){
		//POST varibale here
		$unit_id=@$_POST["unit_id"];
		$unit_id=is_numeric($unit_id)?$unit_id:0;
		$unit_name=trim(@$_POST["unit_name"]);
		$unit_name=htmlentities($unit_name,ENT_QUOTES);
		$unit_active=trim(@$_POST["unit_active"]);
		$unit_active=htmlentities($unit_active,ENT_QUOTES);
		$unit_created_date=trim(@$_POST["unit_created_date"]);
		$unit_created_by=trim(@$_POST["unit_created_by"]);
		$unit_created_by=htmlentities($unit_created_by,ENT_QUOTES);
		$unit_updated_date=trim(@$_POST["unit_updated_date"]);
		$unit_updated_by=trim(@$_POST["unit_updated_by"]);
		$unit_updated_by=htmlentities($unit_updated_by,ENT_QUOTES);
		$unit_updated_count=@$_POST["unit_updated_count"];
		$unit_updated_count=is_numeric($unit_updated_count)?$unit_updated_count:0;
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		$data["type"]="excel";
		$data["data_print"] = $this->m_m_unit->m_unit_print($unit_id ,$unit_name ,$unit_active ,$unit_created_date ,$unit_created_by ,$unit_updated_date ,$unit_updated_by ,$unit_updated_count ,$option,$filter);
		$print_view=$this->load->view("main/p_m_unit.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/m_unit_printlist.xls","w+");
		fwrite($print_file, $print_view);
		echo '1';   
			
	}
	
	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}
		
}
?>